<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()

const showNavigation = computed(() => {
  // 登录页面不显示导航
  return route.name !== 'Login' && route.name !== 'LoginPage'
})

const isAuthenticated = ref(false)

// 检查登录状态
const checkAuthStatus = () => {
  const token = localStorage.getItem('token')
  isAuthenticated.value = !!token
}

// 退出登录
const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('refreshToken')
  localStorage.removeItem('user')
  isAuthenticated.value = false
  router.push('/login')
}

// 导航菜单项
const navigationItems = [
  {
    name: 'Dashboard',
    label: '仪表板',
    icon: '🏠',
    path: '/dashboard'
  },
  {
    name: 'AIAssistant',
    label: 'AI助手',
    icon: '🐱',
    path: '/ai-assistant'
  },
  {
    name: 'QuitProfile',
    label: '戒烟档案',
    icon: '🦊',
    path: '/quit-profile'
  },
  {
    name: 'DailyRecord',
    label: '每日记录',
    icon: '🐰',
    path: '/daily-record'
  },
  {
    name: 'Milestones',
    label: '里程碑',
    icon: '🐢',
    path: '/milestones'
  },
  {
    name: 'UserMilestone',
    label: '我的成就',
    icon: '🦁',
    path: '/achievements'
  },
  {
    name: 'CravingLog',
    label: '烟瘾记录',
    icon: '🦉',
    path: '/craving-log'
  },
  {
    name: 'HealthTips',
    label: '健康建议',
    icon: '🐼',
    path: '/health-tips'
  }
]

// 检查当前路由是否激活
const isActiveRoute = (routeName) => {
  return route.name === routeName
}

onMounted(() => {
  checkAuthStatus()
})
</script>

<template>
  <div class="app">
    <!-- 主导航栏 -->
    <nav v-if="showNavigation" class="main-nav">
      <div class="nav-container">
        <div class="nav-brand">
          <h1>🐼 健康戒烟助手</h1>
        </div>
        
        <div class="nav-menu">
          <router-link
            v-for="item in navigationItems"
            :key="item.name"
            :to="item.path"
            :class="['nav-item', { active: isActiveRoute(item.name) }]"
          >
            <span class="nav-icon">{{ item.icon }}</span>
            <span class="nav-label">{{ item.label }}</span>
          </router-link>
        </div>
        
        <div class="nav-actions">
          <button @click="logout" class="logout-btn">
            👋 退出登录
          </button>
        </div>
      </div>
      
      <!-- 卡通装饰元素 -->
      <div class="nav-decoration">
        <div class="cloud-decoration cloud-1"></div>
        <div class="cloud-decoration cloud-2"></div>
        <div class="star-decoration star-1"></div>
        <div class="star-decoration star-2"></div>
      </div>
    </nav>

    <!-- 主内容区域 -->
    <main :class="['main-content', { 'with-nav': showNavigation }]">
      <router-view />
    </main>
  </div>
</template>

<style scoped>
.app {
  min-height: 100vh;
  background-color: #f5f5f5;
}

/* 导航栏样式 */
.main-nav {
  background: linear-gradient(135deg, #D8EEFF 0%, #FFE0EE 100%);
  color: white;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
  border-radius: 0 0 20px 20px;
  border: 2px solid white;
  backdrop-filter: blur(10px);
  margin: 10px;
  overflow: hidden;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 70px;
  position: relative;
  z-index: 2;
}

.nav-brand h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  font-family: '方正准圆简体', '站酷快乐体', sans-serif;
  color: #444444;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.5);
}

.nav-menu {
  display: flex;
  gap: 5px;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 15px;
  border-radius: 15px;
  text-decoration: none;
  color: #444444;
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  font-size: 14px;
  font-family: '方正准圆简体', '站酷快乐体', sans-serif;
  background: rgba(255, 255, 255, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.5);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
  transform-origin: center;
}

.nav-item:hover {
  color: #444444;
  background: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.nav-item:active {
  transform: scale(0.95);
  transition: all 0.1s ease;
}

.nav-item.active {
  color: #444444;
  background: linear-gradient(135deg, #E4D0FA 0%, #FED0E6 100%);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.8);
}

.nav-icon {
  font-size: 20px;
  transition: all 0.3s ease;
}

.nav-item:hover .nav-icon {
  transform: rotate(10deg) scale(1.2);
}

.nav-label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.nav-actions {
  display: flex;
  align-items: center;
}

.logout-btn {
  background: linear-gradient(135deg, #E4D0FA 0%, #FED0E6 100%);
  color: #444444;
  border: 2px solid white;
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  font-family: '方正准圆简体', '站酷快乐体', sans-serif;
  font-weight: 600;
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  transform-origin: center;
}

.logout-btn:hover {
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.15);
  background: linear-gradient(135deg, #D0B8F0 0%, #FDC0D6 100%);
}

.logout-btn:active {
  transform: scale(0.95);
  transition: all 0.1s ease;
}

/* 卡通装饰元素 */
.nav-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
  overflow: hidden;
}

.cloud-decoration {
  position: absolute;
  background: rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.5);
}

.cloud-1 {
  width: 60px;
  height: 20px;
  top: 10px;
  left: 10%;
  border-radius: 20px;
}

.cloud-2 {
  width: 40px;
  height: 15px;
  bottom: 5px;
  right: 15%;
  border-radius: 15px;
}

.star-decoration {
  position: absolute;
  width: 10px;
  height: 10px;
  background: rgba(255, 255, 255, 0.5);
  clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}

.star-1 {
  top: 15px;
  right: 25%;
}

.star-2 {
  bottom: 10px;
  left: 20%;
}

/* 主内容区域 */
.main-content {
  min-height: calc(100vh - 90px);
}

.main-content.with-nav {
  padding-top: 0;
}

/* 登录页面样式 */
.main-content:not(.with-nav) {
  min-height: 100vh;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    padding: 0 15px;
    height: 60px;
  }
  
  .nav-brand h1 {
    font-size: 18px;
  }
  
  .nav-menu {
    gap: 2px;
  }
  
  .nav-item {
    padding: 6px 10px;
  }
  
  .nav-icon {
    font-size: 18px;
  }
  
  .nav-label {
    font-size: 10px;
  }
  
  .logout-btn {
    padding: 6px 12px;
    font-size: 12px;
  }
  
  .main-content {
    min-height: calc(100vh - 80px);
  }
  
  .main-nav {
    margin: 5px;
  }
}

@media (max-width: 480px) {
  .nav-container {
    flex-direction: column;
    height: auto;
    padding: 10px 15px;
  }
  
  .nav-brand {
    margin-bottom: 10px;
  }
  
  .nav-menu {
    flex-wrap: wrap;
    justify-content: center;
    gap: 5px;
    margin-bottom: 10px;
  }
  
  .nav-item {
    min-width: 70px;
  }
  
  .main-nav {
    border-radius: 0 0 15px 15px;
  }
}
</style>
